﻿using System;
using System.IO;
using System.Linq;
using BloggerAutoPosterService.Engine.Core;
using BloggerAutoPosterService.Engine.Models;
using BloggerAutoPosterService.Engine.Net;
using NUnit.Framework;
using System.Collections.Generic;

namespace BloggerAutoPosterService.Tests
{
    [TestFixture]
    public class TestXmlDB
    {
        IServiceConfig _serviceConfig;
        IRedirectFinder _redirectFinder;

        [TestFixtureSetUp]
        public void Init()
        {
            _serviceConfig = new FakeConfig();
            _redirectFinder = new FakeRedirectFinder();
        }

        [Test]
        public void Test1_XmlDB_AddNewRecord_Should_Add_A_Record()
        {
            new XmlDB(_serviceConfig).AddNewRecord(new NewsRecord
            {
                AddDate = "d1",
                Author = "a1",
                Link = "l1",
                Title = "t1",
                UrlHash = "u1"
            });

            Assert.That(new XmlDB(_serviceConfig).ListNewsRecords(includeDeletedRecords: true).Any());
        }

        [Test]
        public void Test2_XmlDB_AddNewRecord_Should_Not_Add_A_Duplicate_Record()
        {
            if (File.Exists("db.xml"))
                File.Delete("db.xml");

            new XmlDB(_serviceConfig).AddNewRecord(new NewsRecord
            {
                AddDate = "d1",
                Author = "a1",
                Link = "l1",
                Title = "t1",
                UrlHash = "u1"
            });
            new XmlDB(_serviceConfig).AddNewRecord(new NewsRecord
            {
                AddDate = "d1",
                Author = "a1",
                Link = "l1",
                Title = "t1",
                UrlHash = "u1"
            });
            Assert.That(new XmlDB(_serviceConfig).ListNewsRecords(includeDeletedRecords: true).Count() == 1);
        }


        [Test]
        public void Test3_XmlDB_AddNewRecord_Can_Add_Multiple_Records()
        {
            new XmlDB(_serviceConfig).AddNewRecord(new NewsRecord
            {
                AddDate = "d1",
                Author = "a1",
                Link = "l1",
                Title = "t1",
                UrlHash = "u1"
            });
            new XmlDB(_serviceConfig).AddNewRecord(new NewsRecord
            {
                AddDate = "d12",
                Author = "a12",
                Link = "l12",
                Title = "t12",
                UrlHash = "u12"
            });
            Assert.That(new XmlDB(_serviceConfig).ListNewsRecords(includeDeletedRecords: true).Count() == 2);
        }

        [Test]
        public void Test4_XmlDB_AddNewRecord_Can_Append_A_New_Record()
        {
            new XmlDB(_serviceConfig).AddNewRecord(new NewsRecord
            {
                AddDate = "d123",
                Author = "a123",
                Link = "l123",
                Title = "t123",
                UrlHash = "u123"
            });
            Assert.That(new XmlDB(_serviceConfig).ListNewsRecords(includeDeletedRecords: true).Count() == 3);
        }

        [Test]
        public void Test5_XmlDB_AddNewRecord_Can_Save_Feeds_Data()
        {
            new ParseFeedAddToDB(_serviceConfig, _redirectFinder).SaveFeedData();
            Assert.That(File.Exists("db.xml"));
        }

        [Test]
        public void Test6_XmlDB_Can_Be_Exported()
        {
            var data = new ExportData(_serviceConfig).DatesListToHtml(new List<string>
                {
                    DateTime.Now.ToString("dd-MM-yyyy"),
                    DateTime.Now.AddDays(-1).ToString("dd-MM-yyyy")
                });
            File.WriteAllText("tst.htm", data);
            Assert.That(data.Length > 0);
        }

        [Test]
        public void Test7_XmlDB_Can_Delete_Record()
        {
            var record = new NewsRecord
            {
                AddDate = "d1",
                Author = "a1",
                Link = "l1",
                Title = "t1",
                UrlHash = "u1",
                Sent = false,
                IsDeleted = false
            };
            new XmlDB(_serviceConfig).AddNewRecord(record);
            new XmlDB(_serviceConfig).DeleteRecord(record);

            Assert.That(new XmlDB(_serviceConfig).ListNewsRecords(includeDeletedRecords: true).Where(x => x.UrlHash == "u1").FirstOrDefault().IsDeleted);
        }

        [Test]
        public void Test8_XmlDB_MarkAsSent_Should_Work()
        {
            var exportData = new ExportData(_serviceConfig);
            var dates = new List<string>
                            {
                                DateTime.Now.ToString("dd-MM-yyyy"),
                                DateTime.Now.AddDays(-1).ToString("dd-MM-yyyy")
                            };
            var todayList = exportData.DatesToList(dates);
            new XmlDB(_serviceConfig).MarkAsSent(todayList);
            todayList = exportData.DatesToList(dates);
            Assert.That(todayList == null || todayList.Count() == 0);
        }
    }
}
